package com.zeng.sys.filemd.restfulmodule.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecommon.util.MapBuilder;
import com.zeng.nicecommon.util.ResponseUtil;
import com.zeng.sys.authmd.apimodule.vo.UserVo;
import com.zeng.sys.authmd.restfulmodule.util.TokenUtil;
import com.zeng.sys.filemd.servicemodule.transfer.FileHttpTransfer;
import com.zeng.sys.filemd.servicemodule.util.MultipartFileUtil;
import com.zeng.sys.filemd.servicemodule.vo.FileRecordVo;
import com.zeng.sys.filemd.servicemodule.service.FileRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/fileRecord")
@RestController
public class FileRecordController {

    @Autowired
    FileRecordService fileRecordService;

    @Autowired
    MultipartFileUtil multipartFileUtil;

    @RequestMapping("/list")
    public Object list(Page page, FileRecordVo fileRecordVo) {
        List<FileRecordVo> page1 = this.fileRecordService.findPage(page, fileRecordVo);

        ArrayList<Map<String,Object>> objects = new ArrayList<>();
        page1.forEach(e->{
            Map<String, Object> build = MapBuilder.newBuilder()
                    .put("code", e.getCode())
                    .put("updateDate", e.getUpdateDate())
                    .put("insertDate", e.getInsertDate())
                    .put("id", e.getId())
                    .put("name", e.getName())
                    .put("path", e.getPath())
                    .put("size", e.getSize())
                    .put("fileTypeCode", e.getFileTypeCode())
                    .put("fileServerCode", e.getFileServerCode())
                    .put("type", e.getType())
                    .put("status", e.getStatus())
                    .put("uploadUserCode", e.getUploadUserCode())
                    .put("nickName", e.getNickName())
                    .build();
            objects.add(build);

        });
        return ResponseUtil.success("操作成功")
                .addData("fileRecord", objects)
                .addData("page", page);

    }

    @RequestMapping("/detail")
        public Object detail(String code) {

        return ResponseUtil.success("操作成功").addData("fileRecord", fileRecordService.getOneByCode(code));

    }

    @RequestMapping("/add")
    public Object add(FileRecordVo fileRecordVo, MultipartRequest multipartRequest, HttpServletRequest request) {
        System.out.println(multipartRequest);
        UserVo userByToken = TokenUtil.getUserByToken(request);
        if (userByToken == null) {
            return ResponseUtil.noAuthority("请登录");
        }

        multipartFileUtil.handleFileRecordVo(multipartRequest, fileRecordVo, userByToken);
        FileHttpTransfer fileHttpTransfer = new FileHttpTransfer(fileRecordVo.getFileServerVo());
        ResponseUtil upload = fileHttpTransfer.upload(fileRecordVo);
        if (upload != null && upload.getStatus() == 200) {
            return ResponseUtil.success("操作成功").addData("fileRecord", fileRecordService.addOne(fileRecordVo));

        } else {
            return ResponseUtil.serviceError("上传失败");
        }
    }

    @RequestMapping("/update")
    public Object update(FileRecordVo fileRecordVo) {
        Integer update = this.fileRecordService.update(fileRecordVo);

        return ResponseUtil.success("操作成功")
                .addData("updateCount", update);
    }

    @RequestMapping("/delete")
    public Object delete(String code) {
        FileRecordVo oneByCode = fileRecordService.getOneByCode(code);
        if (oneByCode != null) {
            multipartFileUtil.handleFileRecordVoOnServer(oneByCode);
            FileHttpTransfer fileHttpTransfer = new FileHttpTransfer(oneByCode.getFileServerVo());
            ResponseUtil delete = fileHttpTransfer.delete(oneByCode);
            if (delete!= null && delete.getStatus() == 200) {
                return ResponseUtil.success("操作成功").addData("deleteCount", this.fileRecordService.delete(code));
            }
        }
        return ResponseUtil.serviceError("删除失败");

    }
    @RequestMapping("/batchDelete")
    public Object batchDelete(@RequestBody List<String> codes) {
        return fileRecordService.deleteRecordsAndFiles(codes);
    }
}

